<?php
//define('FPDF_FONTPATH','./font/');

/**
** $Author: serge $
** $Revision: 1.3 $
** $Date: 2006/08/11 15:51:31 $
** $Log: CalPrintToPdf.php,v $
** Revision 1.3  2006/08/11 15:51:31  serge
** Preversion, juste avant la premiere phase de test.
**
*/

require 'fpdf.php';

function print_to_pdf($db,  $service_id, $table_name, $id)
{

switch ($table_name)
{

case TB . "contacts";

	print_contact_to_pdf($db,  $service_id, $table_name, $id);
	break;

case TB . "evenements";

	print_evenement_to_pdf($db,  $service_id, $table_name, $id);
	break;

case TB . "cal_list";

	print_cal_list_to_pdf($db,  $service_id, $table_name, $id);
	break;



	} //switch
} //fonction

function print_contact_to_pdf($db,  $service_id, $table_name, $id)
{

	$sql = "SELECT"
		." bl.societe,"
		." bl.nom,"
		." bl.prenom,"
		." bl.contact_type_id,"
		." ty.nom as type,"
		." bl.spec_type,"
		." bl.contact_genre_id,"
		." ge.nom as genre,"
		." bl.adresse,"
		." bl.NP,"
		." bl.ville,"
		." bl.canton,"
		." bl.pays,"
		." bl.tel_prive,"
		." bl.tel_professionnel,"
		." bl.mobile,"
		." bl.url,"
		." bl.mail_liste_id,"
		." ma.nom as mailing_list,"
		." DATE_FORMAT(bl.date_de_naissance,'%d/%m/%Y'),"
		." bl.capacite,"
		." bl.vu_sur,"
		." bl.remarque,"
		." bl.multimedia,"
		." bl.a_voir,"
		." bl.email"
		." FROM " . TB . "contacts bl,"
		."     " . TB . "contact_types ty,"
		."     " . TB . "contact_genres ge,"
		."     " . TB . "mail_listes ma"
		." WHERE ty.id = bl.contact_type_id"
		." AND   ge.id = bl.contact_genre_id"
		." AND   ma.id = bl.mail_liste_id"
		." AND bl.id=" . $id;

	deb_echo ("La query de visualisation est : " . $sql);

	/* assignation dans variables session */
	if (is_array($row = $db->getRow($sql))) {
		list(
			$societe,
			$nom,
			$prenom,
			$contact_type_id,
			$type,
			$spec_type,
			$contact_genre_id,
			$genre,
			$adresse,
			$NP,
			$ville,
			$canton,
			$pays,
			$tel_prive,
			$tel_professionnel,
			$mobile,
			$url,
			$mail_liste_id,
			$mailing_list,
			$date_de_naissance,
			$capacite,
			$vu_sur,
			$remarque,
			$multimedia,
			$a_voir,
			$email) = $row;
		}


	$pos_col_1=10;
	$pos_col_2=60;
	$line_gap=10;
	$line_pos=20;

	$pdf=new FPDF();

	//Logo

	$pdf->AddPage();
	$pdf->Image(IMG_LOGO_S_SMAL,10,8,33);

	$pdf->SetFont('Times','B',16);
	$cur_line=20;
	$pdf->Text(50,$cur_line,"Contact");

	$pdf->SetFont('Times','',12);
	$cur_line+=$line_gap;
	$cur_line+=$line_gap;

	if ( $societe != "" ){
		$pdf->Text($pos_col_1,$cur_line,"Societe");
		$pdf->Text($pos_col_2,$cur_line, $societe);
		$cur_line+=$line_gap;
		$pdf->Text($pos_col_1,$cur_line,"Type");
		if ( $contact_type_id == 1 ){
			$pdf->Text($pos_col_2,$cur_line, $spec_type);
		}else{
			$pdf->Text($pos_col_2,$cur_line, $type);
		}
		$cur_line+=$line_gap;
	}

	if ( $nom != "" and $prenom != "") {
	$pdf->Text($pos_col_1,$cur_line,"Nom");
	$pdf->Text($pos_col_2,$cur_line,$nom . " " . $prenom);
	$cur_line+=$line_gap;
	}


	$pdf->SetXY($pos_col_1,$cur_line);
	$pdf->Cell(0,0,"Adresse");
	$pdf->SetXY($pos_col_2,$cur_line);
	$pdf->MultiCell(120,5, wordwrap(strip_tags($adresse ."\n" . $NP . " " . $ville . "\n" . $canton . " (" . $pays. ")"), 120, "\n") , 0 ,L  );
	$cur_line=$pdf->GetY()+$line_gap;

	$pdf->Text($pos_col_1,$cur_line,"Tel. prive");
	$pdf->Text($pos_col_2,$cur_line,$tel_prive);
	$cur_line+=$line_gap;

	$pdf->Text($pos_col_1,$cur_line,"Tel. Prof.");
	$pdf->Text($pos_col_2,$cur_line,$tel_professionnel);
	$cur_line+=$line_gap;

	$pdf->Text($pos_col_1,$cur_line,"Mobile");
	$pdf->Text($pos_col_2,$cur_line,$mobile);
	$cur_line+=$line_gap;
	$pdf->Text($pos_col_1,$cur_line,"URL");
	$pdf->Text($pos_col_2,$cur_line,$url);
	$cur_line+=$line_gap;

	$pdf->Text($pos_col_1,$cur_line,"Mailing");
	$pdf->Text($pos_col_2,$cur_line,$mailing_list);
	$cur_line+=$line_gap;

	$pdf->Text($pos_col_1,$cur_line,"Date de naissance");
	$pdf->Text($pos_col_2,$cur_line,$date_de_naissance);
	$cur_line+=$line_gap;

	$pdf->Text($pos_col_1,$cur_line,"Capacite");
	$pdf->Text($pos_col_2,$cur_line,$capacite);
	$cur_line+=$line_gap;

	$pdf->Text($pos_col_1,$cur_line,"Vu sur...");
	$pdf->Text($pos_col_2,$cur_line,$vu_sur);
	$cur_line+=$line_gap;

	$pdf->SetXY($pos_col_1,$cur_line);
	$pdf->Cell(0,0,"Remarque");
	$pdf->SetXY($pos_col_2,$cur_line);
	$pdf->MultiCell(120,5, wordwrap(strip_tags($remarque), 120, "\n") , 0 ,L  );
	$cur_line=$pdf->GetY()+$line_gap;

	$pdf->SetXY($pos_col_1,$cur_line);
	$pdf->Cell(0,0,"Multimedia");
	$pdf->SetXY($pos_col_2,$cur_line);
	$pdf->MultiCell(120,5, wordwrap(strip_tags($multimedia), 120, "\n") , 0 ,L  );
	$cur_line=$pdf->GetY()+$line_gap;

	$pdf->SetXY($pos_col_1,$cur_line);
	$pdf->Cell(0,0,"A voir...");
	$pdf->SetXY($pos_col_2,$cur_line);
	$pdf->MultiCell(120,5, wordwrap(strip_tags($a_voir), 120, "\n") , 0 ,L  );
	$cur_line=$pdf->GetY()+$line_gap;

	$pdf->Text($pos_col_1,$cur_line,"Email");
	$pdf->Text($pos_col_2,$cur_line,$email);
	$cur_line+=$line_gap;

	$filename="pdftemp/contact" . $id . ".pdf";
	$label=MSG_PRINT_OK;
	$pdf->Output($filename,"F");

	go_back($filename,$label,1);

} // fonction


function print_evenement_to_pdf($db,  $service_id, $table_name, $id)
{
			/* definir la query */
	        $sql = 'SELECT ev.id,
					ev.utilisateur_id,
					ev.titre,
					ev.ev_date,
					ev.duree,
					ev.description,
					ev.audience,
					ev.type_id,
					ev.projet_id,
					ev.prive,
					ev.tache_terminee,
					ev.rep_periodique,
					ev.rep_type,
					ev.rep_duree,
					ev.rep_parent_id,
					ev.rep_date_fin,
	        		usr.username,
	        		ut.nom as utilisateur_type,
	        		pr.nom as project,
	        		et.nom as evenement_type,
					DATE_FORMAT(ev.duree, "%Hh%i") as treated_duree,
					DATE_FORMAT(ev.ev_date,"%d/%m/%Y") as treated_ev_date,
					DATE_FORMAT(ev.ev_date,"%Hh%i") as treated_ev_heure
	        		FROM ' . TB . 'evenements ev, ' . TB . 'evenement_types et, ' . TB . 'utilisateurs usr, ' . TB . 'utilisateur_types ut, ' . TB . 'projets pr
	        		WHERE ev.id =  ' . $id . '
	        		AND usr.id = ev.utilisateur_id AND ut.id = ev.audience AND pr.id = ev.projet_id AND ev.type_id = et.id';

	        /* lancer la query */
	        $res = $db->query($sql);

	        /* pas d'erreur base de donnees */
	        /* assignation dans variables session */
	        if (is_array($row = $db->getRow($sql))) {
	            list(
				$id,
				$utilisateur_id,
				$titre,
				$ev_date,
				$duree,
				$description,
				$audience,
				$type_id,
				$projet_id,
				$prive,
				$tache_terminee,
				$rep_periodique,
				$rep_type,
				$rep_duree,
				$rep_parent_id,
				$rep_date_fin,
	            $username,
	            $utilisateur_type,
	            $projet,
	            $evenement_type,
				$treated_duree,
				$treated_ev_date,
				$treated_ev_heure
	        ) = $row;
		}


	$pos_col_1=10;
	$pos_col_2=60;
	$line_gap=10;
	$line_pos=20;


	$pdf=new FPDF();

	//Logo

	$pdf->AddPage();
	$pdf->Image(IMG_LOGO_S_SMAL,10,8,33);

	$pdf->SetFont('Times','B',16);
	$cur_line=20;
	$pdf->Text(50,$cur_line,"Evenement : $titre");

	$pdf->SetFont('Times','',12);
	$cur_line+=$line_gap;
	$cur_line+=$line_gap;

	$pdf->SetXY($pos_col_1,$cur_line);
	$pdf->Cell(0,0,"Description");
	$pdf->SetXY($pos_col_2,$cur_line);
	$pdf->MultiCell(120,5, wordwrap(strip_tags($description), 120, "\n") , 0 ,L  );
	$cur_line=$pdf->GetY()+$line_gap;

	$pdf->Text($pos_col_1,$cur_line,"Date");
	$pdf->Text($pos_col_2,$cur_line, $treated_ev_date);
	$cur_line+=$line_gap;

	$pdf->Text($pos_col_1,$cur_line,"Heure");
	$pdf->Text($pos_col_2,$cur_line, $treated_ev_heure);
	$cur_line+=$line_gap;

	$pdf->Text($pos_col_1,$cur_line,"Duree");
	$pdf->Text($pos_col_2,$cur_line, $treated_duree );
	$cur_line+=$line_gap;

	$pdf->Text($pos_col_1,$cur_line,"Createur");
	$pdf->Text($pos_col_2,$cur_line, $username );
	$cur_line+=$line_gap;

	$pdf->Text($pos_col_1,$cur_line,"Audience");
	$pdf->Text($pos_col_2,$cur_line, $utilisateur_type );
	$cur_line+=$line_gap;

	$pdf->Text($pos_col_1,$cur_line,"Type");
	$pdf->Text($pos_col_2,$cur_line, $evenement_type );
	$cur_line+=$line_gap;

	$pdf->Text($pos_col_1,$cur_line,"Projet");
	$pdf->Text($pos_col_2,$cur_line, $projet );
	$cur_line+=$line_gap;

	if ( $rep_periodique == 1 ){

		$pdf->Text($pos_col_1,$cur_line,"Repetition");
		$pdf->Text($pos_col_2,$cur_line, $rep_type);
		$cur_line+=$line_gap;

		$pdf->Text($pos_col_1,$cur_line,"Jusqu'au");
		$pdf->Text($pos_col_2,$cur_line, $rep_date_fin );
		$cur_line+=$line_gap;

	}else{

		$pdf->Text($pos_col_1,$cur_line,"Repetition");
		$pdf->Text($pos_col_2,$cur_line, "Aucune");
		$cur_line+=$line_gap;
	}

	$filename="pdftemp/evenement" . $id . ".pdf";
	$label=MSG_PRINT_OK;
	$pdf->Output($filename,"F");

	go_back($filename,$label,1);
} // function


function print_cal_list_to_pdf($db,  $service_id, $table_name, $id){

	/* check des droits */
	if ($_SESSION['ponderation'] < 10)
		$has_rights = "no";
	else
		$has_rights = "yes";


	/* initialisation de la date du jour */
	$today = getdate();
	$today_month = $today['mon'];
	$today_day = $today['mday'];
	$today_year = $today['year'];


	if (isset($_GET["n_day"]))
		$n_day = $_GET['n_day'];
	else 
		$n_day="";
		
	if (isset($_GET["n_month"]))
		$n_month = $_GET['n_month'];
	else 
		$n_month="";		
	
	if (isset($_GET["n_year"]))
		$n_year = $_GET['n_year'];
	else 
		$n_year="";
		

	if ($today_day == $n_day and $today_month == $n_month and $today_year == $n_year){
		$today_color="rgb(182,213,234)";
	}else{
		$today_color="rgb(224, 224, 224)";
	}

	/* get session variables */
	$date_debut = $_SESSION['date_debut'];
	$date_fin = $_SESSION['date_fin'];
	$type_query = $_SESSION['type_query'];

	$title_begin="Liste des evenements et taches  pour la periode du ";

	/* definition de la query avec verification des droits */
	$sql = "SELECT ev.id , CONCAT( LEFT(ev.titre, 80) ,  '...'  )  AS cut_titre,  usr.username,
	DATE_FORMAT(ev.duree, '%Hh%i') as duree, usr.prenom, et.nom AS type, ev.rep_periodique,
	DATE_FORMAT(ev.ev_date,'%d/%m/%Y %Hh%i') as treated_ev_date,
	DATE_FORMAT(ev.ev_date,'%Y%m%d%Hh%i') as filter_ev_date,
	ev.type_id, ev.tache_terminee, ev.rep_parent_id,  CONCAT( LEFT(ev.description, 45) ,  '...'  )  AS description
	FROM " . TB . "evenements ev, " . TB . "utilisateur_types ut, " . TB . "utilisateurs usr, " . TB . "evenement_types et
	WHERE ev_date >= '" . $date_debut["Y"] . "-" . $date_debut["M"] . "-" . $date_debut["d"] . " 00:00:00'
	AND ev_date <= '" . $date_fin["Y"] . "-" . $date_fin["M"] . "-" . $date_fin["d"] . " 23:59:59'
	AND ( ( ev.prive = 1 AND ev.utilisateur_id = " . $_SESSION['user_id'] . " )
	OR ( ev.prive = 0 AND ut.ponderation <= " . $_SESSION['ponderation'] ." )
	OR '". $_SESSION['ponderation'] ."' >= 10)
	AND ev.audience = ut.id
	AND ev.utilisateur_id = usr.id
	AND ev.type_id = et.id
	ORDER BY filter_ev_date";

	$title_str = $title_begin . $date_debut["d"] . "/" . $date_debut["M"] . "/" . $date_debut["Y"] .
	" au " . $date_fin["d"] . "/" . $date_fin["M"] . "/" . $date_fin["Y"] ;

	/* on execute la query */
	$result = $db->query($sql);


	/* si resutls */
	if ( DB::isError( $result = $db->query( $sql ) ) ) {
	    // on fait rien
	}else{

		$pos_col_1=10;
		$pos_col_2=40;
		$pos_col_3=80;
		$pos_col_4=100;
		$pos_col_5=180;
		$pos_col_6=200;
		$line_gap=10;
		$line_pos=20;
		$cur_page=1;

		$pdf=new FPDF();

		//Logo

		$pdf->AddPage(L);
		
		// footer
		$pdf->SetFont('Times','B',16);
		$pdf->Line(10, 200, 290, 200);
		$pdf->SetFont('Times','',10);
		$pdf->Text(140,205,"Page : " . $pdf->PageNo());




		$pdf->Image(IMG_LOGO_S_SMAL,10,8,33);

		$pdf->SetFont('Times','B',16);
		$cur_line=10;

		$pdf->SetXY(50,$cur_line);
		$pdf->MultiCell(120,5, wordwrap(strip_tags($title_str), 80, "\n") , 0 ,L  );
		$cur_line=$pdf->GetY()+$line_gap;

		$pdf->SetFont('Times','',14);

		$pdf->Text($pos_col_1,$cur_line,"Type");
		$pdf->Text($pos_col_2,$cur_line,"Date");
		$pdf->Text($pos_col_3,$cur_line,"Duree");
		$pdf->Text($pos_col_4,$cur_line,"Titre");
		$pdf->Text($pos_col_6,$cur_line,"Description");

			$pdf->Line(10, $cur_line+4, 290, $cur_line+4);
			$cur_line+=$line_gap;

		$pdf->SetFont('Times','',12);

		while ($row = $result->fetchrow(DB_FETCHMODE_ASSOC)) {
		$pdf->SetFont('Times','',12);
		$pdf->Text($pos_col_1,$cur_line,$row["type"]);
		$pdf->Text($pos_col_2,$cur_line,$row["treated_ev_date"]);
		$pdf->Text($pos_col_3,$cur_line,$row["duree"]);
		$pdf->Text($pos_col_4,$cur_line,$row["cut_titre"]);
		$pdf->Text($pos_col_5,$cur_line,$row["username"]);
		$pdf->Text($pos_col_6,$cur_line,strip_tags($row["description"]));
		$cur_line+=$line_gap;

		if ($cur_line == 280) {

				$pdf->SetFont('Times','',10);
				$pdf->Line(10, 284, 200, 284);
				$pdf->Text(100,290,"Page : " . $cur_page);
				$pdf->AddPage();
				$cur_page+=1;
				$pdf->Image(IMG_LOGO_S_SMAL,10,8,33);

				// footer
				$pdf->SetFont('Times','B',16);
				$pdf->Line(10, 200, 290, 200);
				$pdf->SetFont('Times','',10);
				$pdf->Text(140,205,"Page : " . $pdf->PageNo());


				$pdf->SetFont('Times','B',16);
				$cur_line=10;

				$pdf->SetXY(50,$cur_line);
				$pdf->MultiCell(120,5, wordwrap(strip_tags($title_str), 80, "\n") , 0 ,L  );
				$cur_line=$pdf->GetY()+$line_gap;

				$pdf->SetFont('Times','',14);

				$pdf->Text($pos_col_1,$cur_line,"Type");
				$pdf->Text($pos_col_2,$cur_line,"Date");
				$pdf->Text($pos_col_3,$cur_line,"Duree");
				$pdf->Text($pos_col_4,$cur_line,"Titre");
				$pdf->Text($pos_col_5,$cur_line,"Pseudo");
				$pdf->Text($pos_col_6,$cur_line,"Description");

				$pdf->Line(10, $cur_line+4, 290, $cur_line+4);
				$cur_line+=$line_gap;
		}
	} //while
	$pdf->SetFont('Times','',10);
	$pdf->Line(10, 284, 200, 284);
	$pdf->Text(100,290,"Page : " . $cur_page);

	$filename="pdftemp/evenement_liste_" . md5($_SESSION['username']) . ".pdf";
	$label=MSG_PRINT_OK;
	$pdf->Output($filename,"F");

	go_back($filename,$label,1);


	} // if

}

function print_to_pdf_cal($db,  $service_id, $n_day, $n_month, $n_year)
{
    /* initialisation de la date en fonction des parametres */
	$today_month = translate_month ($n_month);
	$today_day = $n_day;
	$today_year = $n_year;

	/* special treatment fo dates */
	if ( $n_month == 1 ){
	    $n_month_minus=12;
	    $n_year_minus=$n_year - 1;
	}else{
	    $n_month_minus=$n_month - 1;
	    $n_year_minus=$n_year;
	}
	if ( $n_month == 12 ){
	    $n_month_plus=1;
	    $n_year_plus=$n_year + 1;
	}else{
	    $n_month_plus=$n_month + 1;
	    $n_year_plus=$n_year;
	}


	// header
	$pos_col_1=05;
	$col_width=40;
	$pos_col_2=$pos_col_1+$col_width;
	$pos_col_3=$pos_col_2+$col_width;
	$pos_col_4=$pos_col_3+$col_width;
	$pos_col_5=$pos_col_4+$col_width;
	$pos_col_6=$pos_col_5+$col_width;
	$pos_col_7=$pos_col_6+$col_width;

	$line_gap=10;
	$line_pos=20;
	$cur_page=1;

	$pdf=new FPDF();

	print_to_pdf_cal_header($db,  $service_id, $n_day, $n_month, $n_year, &$pdf, &$cur_line);

	print_to_pdf_cal_content($db,  $service_id, $n_day, $n_month, $n_year, &$pdf, &$cur_line);

	// footer
	$pdf->SetFont('Times','',10);
	$pdf->Line(5, 200, 285, 200);
	$pdf->Text(140,205,"Page : " . $pdf->PageNo());

	$filename="pdftemp/calendare_month_" . md5($_SESSION['username']) . ".pdf";
	$label=MSG_PRINT_OK;
	$pdf->Output($filename,"F");

	go_back($filename,$label,1);


}

function print_to_pdf_cal_header($db,  $service_id, $n_day, $n_month, $n_year, &$pdf, &$cur_line)
{

	$pos_col_1=05;
	$col_width=40;
	$pos_col_2=$pos_col_1+$col_width;
	$pos_col_3=$pos_col_2+$col_width;
	$pos_col_4=$pos_col_3+$col_width;
	$pos_col_5=$pos_col_4+$col_width;
	$pos_col_6=$pos_col_5+$col_width;
	$pos_col_7=$pos_col_6+$col_width;

	$line_gap=10;
	$line_pos=20;
	$cur_page=1;


	$pdf->AddPage(L);

	$pdf->Image(IMG_LOGO_S_SMAL,10,8,33);
	$pdf->SetFont('Times','B',16);
	$cur_line=15;

	$pdf->SetXY(50,$cur_line);
	$pdf->MultiCell(120,5, "Calendrier, mois : " .translate_month ($n_month) . " " . $n_year , 0 ,L  );
	$cur_line=$pdf->GetY()+$line_gap;

	$pdf->SetFont('Times','',14);

	$cur_line+=$line_gap-10;

	$pdf->Text($pos_col_1,$cur_line,"Lundi");
	$pdf->Text($pos_col_2,$cur_line,"Mardi");
	$pdf->Text($pos_col_3,$cur_line,"Mercredi");
	$pdf->Text($pos_col_4,$cur_line,"Jeudi");
	$pdf->Text($pos_col_5,$cur_line,"Vendredi");
	$pdf->Text($pos_col_6,$cur_line,"Samedi");
	$pdf->Text($pos_col_7,$cur_line,"Dimanche");

	$pdf->Line(5, $cur_line+2, $pos_col_7+$col_width , $cur_line+2);
//	$cur_line+=$line_gap;
}


/*
** Function : print_cal_content
**
** Deal : creation du contenu du calendrier
**
** Params : database connection, based form name, service id, dates.
*/
function print_to_pdf_cal_content($db,  $service_id, $n_day, $n_month, $n_year, &$pdf, &$cur_line)
{

	// header
	$pos_col=05;
	$pos_col_1=05;
	$col_width=40;
	$line_gap=10;
	$line_pos=50;
	$cur_page=1;
	$max_line=0;


	$cur_line+=$line_gap;

	if ($_SESSION['ponderation'] < 10)
		$has_rights = "no";
	else
		$has_rights = "yes";


	$Month = new Calendar_Month_Weekdays($n_year, $n_month);
	$Month->build();

	while ($Day = $Month->fetch()) {

		if ($Day->isFirst()) {
			$pos_col=05;

	    }else{

			$pos_col+=$col_width;
		}

		if ($Day->isEmpty()) {
	    } else {
	        // on appelle la fonction print_cal_cell pour chaque jours
			print_to_pdf_cal_cell($db,  $service_id, $Day->thisDay(), $n_month, $n_year, &$pdf, $cur_line, $pos_col,&$max_line);

			if ($loc_max_line < $max_line) {
				$loc_max_line=$max_line + $line_gap;
			}

	    }
	    if ($Day->isLast()) {
				$cur_line=$loc_max_line;
				$loc_max_line=0;
				if ($cur_line > 150 ) {

					// footer
					$pdf->SetFont('Times','',10);
					$pdf->Line(5, 200, 285, 200);
					$pdf->Text(140,205,"Page : " . $pdf->PageNo());

					print_to_pdf_cal_header($db,  $service_id, $n_day, $n_month, $n_year, &$pdf, &$cur_line);
					$cur_line+=$line_gap;
				}
	    }
	}
}

/*
** Function : print_cal_cell
**
** Deal : print de chaque case du calendrier
**
** Params : database connection, based form name, service id, dates.
*/
function print_to_pdf_cal_cell($db,  $service_id, $n_day, $n_month, $n_year, &$pdf, $cur_line, $cur_col, &$max_line)
{

	/* check des droits */
	/* initialisation de la date du jour */
	$today = getdate();
	$today_month = $today['mon'];
	$today_day = $today['mday'];
	$today_year = $today['year'];


	if ($today_day == $n_day and $today_month == $n_month and $today_year == $n_year){
		$today_color="rgb(182,213,234)";
	}else{
		$today_color="rgb(224, 224, 224)";
	}

	/* definition de la query avec verification des droits */
	$sql = "SELECT
				ev.id,
				ev.utilisateur_id,
				ev.titre,
				ev.ev_date,
				ev.duree,
				CONCAT( LEFT( ev.description, 200 ) , '...' ) AS cut_description,
				ev.audience,
				ev.type_id,
				ev.projet_id,
				ev.prive,
				ev.tache_terminee,
				ev.rep_periodique,
				ev.rep_type,
				ev.rep_duree,
				ev.rep_parent_id,
				ev.rep_date_fin,
				usr.username,
				ut.nom as utilisateur_type,
				pr.nom as project,
				et.nom as evenement_type,

				CONCAT( LEFT( ev.titre, 40 ) , '...' ) AS cut_titre,
				DATE_FORMAT(ev_date, '%H') as heure,
				DATE_FORMAT(ev_date, '%i') as minute

			FROM " . TB . "evenements ev,
				" . TB . "utilisateur_types ut,
				" . TB . "utilisateurs usr,
				" . TB . "projets pr,
				" . TB . "evenement_types et

			WHERE ev_date >= '" . $n_year . "-" . $n_month . "-" . $n_day . " 00:00:00'
				AND ev_date <= '" . $n_year . "-" . $n_month . "-" . $n_day . " 23:59:59'
				AND ( ( ev.prive = 1 AND ev.utilisateur_id = " . $_SESSION['user_id'] . " )
				OR ( ev.prive = 0 AND ut.ponderation <= " . $_SESSION['ponderation'] ." )
				OR '". $_SESSION['ponderation'] ."' >= 10)
				AND ev.audience = ut.id
				AND usr.id = ev.utilisateur_id
				AND ut.id = ev.audience
				AND pr.id = ev.projet_id
				AND ev.type_id = et.id

			ORDER BY heure, minute";
	/* on execute la query */
	$result = $db->query($sql);

	/* si resutls */
	if ( DB::isError( $result = $db->query( $sql ) ) ) {
	    // on fait rien
	}else{

		// header
		$pos_col=05;
		$pos_col_1=05;
		$col_width=50;
		$line_gap=5;
		$line_pos=40;
		$cur_page=1;

		$loc_cur_line=$cur_line;

		$pdf->SetFont('Times','',8);
		$pdf->Text($cur_col,$loc_cur_line,$n_day);
	    $pdf->Line($cur_col+2,$cur_line+1, $cur_col+$col_width-10,$cur_line+1);

		$loc_cur_line+=$line_gap+1;

		while ($row = $result->fetchrow(DB_FETCHMODE_ASSOC)) {

		$pdf->SetXY($cur_col+2,$loc_cur_line-4);
		$cal_str=$row["heure"] . "h" . $row["minute"] . " ". strip_tags($row["cut_titre"])
		. "\n" . "Duree : " . $row["duree"]. " Pseudo : " .  $row["username"]
		. "\n" . "Desc : " . strip_tags($row["cut_description"]);

		$pdf->MultiCell($col_width-10,3,  $cal_str  , 0 ,L);

		$loc_cur_line=$pdf->GetY()+$line_gap;
    	 $pdf->Line($cur_col+5,$pdf->GetY(), $cur_col+$col_width-20,$pdf->GetY());

		} //while
	} // if db

	$max_line=$loc_cur_line+$line_gap;
}

?>